package com.rms.controller;

import com.alibaba.fastjson.JSONObject;
import com.rms.entity.Cuisine;
import com.rms.service.CuisineService;
import com.rms.service.DishService;
import com.rms.utils.UserLoginToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 胡满意
 * @date 2019/10/31
 */
@RestController
public class CuisineController {
    @Autowired
    private CuisineService cuisineService;
    @Autowired
    private DishService dishService;

    @GetMapping("/cuisine")
    @UserLoginToken
    public JSONObject selectAll(){
        JSONObject object = new JSONObject();
        try {
            List<Cuisine> cuisineList = cuisineService.selectAll();
            object.put("cuisineList",cuisineList);
            object.put("state","success");
            object.put("message","菜系获取成功");
            return object;
        }catch (Exception e){
            e.printStackTrace();
            object.put("state","error");
            object.put("message","菜系获取失败");
            return object;
        }
    }

    @PostMapping("/cuisine")
    @UserLoginToken
    public JSONObject insertCuisine(String cuisineName){
        JSONObject object = new JSONObject();
        try {
            if (cuisineService.selectCuisine(cuisineName)==null){
                if (cuisineService.insertCuisine(cuisineName)>0){
                    object.put("state","success");
                    object.put("message","菜系添加成功");
                    return object;
                }
                object.put("state1","error");
                object.put("message","菜系添加失败");
                return object;
            }
            object.put("state1","error");
            object.put("message","菜系已存在");
            return object;
        }catch (Exception e){
            e.printStackTrace();
            object.put("state1","error");
            object.put("message","菜系添加失败");
            return object;
        }
    }

    @PutMapping("/cuisine")
    @UserLoginToken
    public JSONObject updateCuisine(int cuisineId,String cuisineName){
        JSONObject object = new JSONObject();
        try {
            if (cuisineService.selectCuisine(cuisineName)==null){
                if (cuisineService.updateCuisine(cuisineId,cuisineName)>0){
                    object.put("state","success");
                    object.put("message","菜系更新成功");
                    return object;
                }
                object.put("state1","error");
                object.put("message","菜系更新失败");
                return object;
            }
            object.put("state1","error");
            object.put("message","菜系已存在");
            return object;
        }catch (Exception e){
            e.printStackTrace();
            object.put("state1","error");
            object.put("message","菜系更新失败");
            return object;
        }
    }

    @DeleteMapping("/cuisine")
    @UserLoginToken
    public JSONObject deleteCuisine(int cuisineId){
        JSONObject object = new JSONObject();
        try {
            if (dishService.cuisineDelete(cuisineId)>0&&cuisineService.deleteCuisine(cuisineId)>0){
                object.put("state","success");
                object.put("message","菜系删除成功");
                return object;
            }
            object.put("state","error");
            object.put("message","菜系删除失败");
            return object;
        }catch (Exception e){
            e.printStackTrace();
            object.put("state","error");
            object.put("message","菜系删除失败");
            return object;
        }
    }
}
